home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_11 / phillip2 / cips4.c < prev    next >
C/C++ Source or Header  |  1993-08-22  |  42KB  |  1,574 lines

  1.  
  2.    /*************************** 
  3.    * 
  4.    *   cips4.c 
  5.    *   COMPOSITE FILE COMPRISING: 
  6.    *   gin.c 
  7.    *   hist.c 
  8.    *   pi.c 
  9.    *   ht.c 
  10.    * 
  11.    ***************************\ 
  12.  
  13.  
  14.  
  15.         /*******************************************************
  16.         *
  17.         *       file d:\cips\gin.c
  18.         *
  19.         *       Functions: This file contains
  20.         *           get_image_name
  21.         *           get_directory_name
  22.         *           extract_base_image_name
  23.         *
  24.         *       Purpose - This function prompts the user to
  25.           *            enter the name of an image.
  26.         *
  27.         *       External Calls:
  28.         *                       clear_buffer
  29.         *
  30.         *       Modifications:
  31.         *           26 September 86 - now uses vision3.h
  32.         *               instead of vision2.h and the read_string
  33.         *               and get_integer instead of  scanf.
  34.         *           11 March 1987 - this function was
  35.         *               removed from the file ip.c and put
  36.         *               in file gin.c.
  37.         *
  38.         ******************************************************/
  39.  
  40.  
  41. #include "cips.h"
  42.  
  43.  
  44.  
  45.  
  46.    /*********************************************
  47.     *
  48.     * get_image_name(...
  49.     *
  50.     * This function reads in the desired image
  51.     * file name.
  52.     *
  53.     *********************************************/
  54.  
  55. get_image_name(name)
  56.    char name[];
  57. {
  58.    char base_name[80],
  59.         dir_name[80],
  60.         new_name[80],
  61.         response[80];
  62.    int  l;
  63.  
  64.    printf("\n\nImage name is--%s\n", name);
  65.    printf("\nDo you want to change:");
  66.    printf("\n (f) file name");
  67.    printf("\n (d) directory name");
  68.    printf("\n (n) no change");
  69.    printf("\n     _\b");
  70.    gets(response);
  71.  
  72.    if((response[0] == 'F') ||
  73.       (response[0] == 'f')){
  74.       printf("\n\nEnter file name (name only no extension)");
  75.       printf("\n--");
  76.       gets(new_name);
  77.       extract_directory_name(name, dir_name);
  78.       sprintf(name, "%s%s.tif", dir_name, new_name);
  79.    }
  80.  
  81.    if((response[0] == 'D') ||
  82.       (response[0] == 'd')){
  83.       printf("\n\nEnter directory name\n--");
  84.       gets(dir_name);
  85.       l = strlen(dir_name);
  86.       if(dir_name[l-1] != 47){
  87.          dir_name[l]   = '/';
  88.          dir_name[l+1] = '\0';
  89.       }
  90.       printf("\n\nEnter file name (name only no extension)");
  91.       printf("\n--");
  92.       gets(new_name);
  93.       sprintf(name, "%s%s.tif", dir_name, new_name);
  94.    }
  95.  
  96. }       /* ends get_image_name  */
  97.  
  98.  
  99.  
  100.  
  101.    /*********************************************
  102.     *
  103.     * extract_directory_name(...
  104.     *
  105.     * This function extracts the sub-directory
  106.     * name out of a file name.
  107.     *
  108.     *********************************************/
  109.  
  110. extract_directory_name(file_name, dir_name)
  111.    char file_name[], dir_name[];
  112. {
  113.    int i, j, k;
  114.  
  115.    i = 1;
  116.    j = 0;
  117.    k = 0;
  118.    while(i){
  119.       if(file_name[k] == 47  ||
  120.          file_name[k] == 92)     j = k;
  121.       if(file_name[k] == '\0')   i = 0;
  122.       k++;
  123.    }
  124.    j++;
  125.    strncpy(dir_name, file_name, j);
  126.    dir_name[j] = '\0';
  127.  
  128. }  /* ends extract_directory_name */
  129.  
  130.  
  131.  
  132.  
  133.  
  134.    /*********************************************
  135.     *
  136.     *   extract_base_image_name(...
  137.     *
  138.     *   This function looks at a full file name
  139.     *   and pulls off the sub-directory name and
  140.     *   the file extension and returns the base
  141.     *   file name.
  142.     *
  143.     *********************************************/
  144.  
  145. extract_base_file_name(file_name, base_name)
  146.    char base_name[], file_name[];
  147. {
  148.    int i, j, k;
  149.    i = 1;
  150.    j = 0;
  151.    k = 0;
  152.    while(i){
  153.       if(file_name[k] == 47  ||
  154.          file_name[k] == 92)     j = k;
  155.       if(file_name[k] == '\0')   i = 0;
  156.       k++;
  157.    }
  158.  
  159.    i = 1;
  160.    k = 0;
  161.    j++;
  162.    while(i){
  163.       if(file_name[j] == '.')
  164.          i = 0;
  165.       else
  166.          base_name[k] = file_name[j];
  167.       j++;
  168.       k++;
  169.    }
  170.    k--;
  171.     base_name[k] = '\0';
  172. printf("\nEBN> base is %s", base_name);
  173. }  /* ends extract_base_file_name */
  174.  
  175.    /**************************************************
  176.    *
  177.    *   file d:\cips\hist.c
  178.    *
  179.    *   Functions: This file contains
  180.    *       calculate_histogram
  181.    *       calculate_histogram
  182.    *       zero_histogram
  183.    *       perform_histogram_equalization
  184.    *       show_histogram
  185.    *       print_histogram
  186.    *       smooth_histogram
  187.    *       display_histogram
  188.    *
  189.    *   Purpose: These functions calculate and display 
  190.    *      the histogram of an input image array.
  191.    *
  192.    *   Modifications:
  193.    *       July 86 - ported to IBM-PC
  194.    *       August 1990 - modified for use in the
  195.    *           C Image Processing System
  196.    *       March 1992 - removed the hardwired values
  197.    *           of 100 and replaced them with ROWS
  198.    *           and COLS.  There are still some
  199.    *           hardwired numbers in this file, but
  200.    *           they deal with displaying a histogram.
  201.    *       October 4, 1992 - added the smooth histogram
  202.    *           function.
  203.    *
  204.    **************************************************/
  205.  
  206.  
  207.  
  208. #define PRINT_WIDTH  80
  209. #define FORMFEED     '\014'
  210.  
  211.  
  212.  
  213.  
  214.           /*****************************************
  215.           *
  216.           *    perform_histogram_equalization(...
  217.           *
  218.           *    This function performs histogram
  219.           *    equalization on the input image array.
  220.           *
  221.           ******************************************/
  222.  
  223. perform_histogram_equalization(image, histogram,
  224.                                new_grays, area)
  225.    float new_grays, area;
  226.    short image[ROWS][COLS];
  227.    unsigned long histogram[];
  228. {
  229.    int i,
  230.        j,
  231.        k;
  232.    unsigned long sum,
  233.             sum_of_h[256];
  234.  
  235.    double constant;
  236.  
  237.    sum = 0;
  238.    for(i=0; i<256; i++){
  239.       sum         = sum + histogram[i];
  240.       sum_of_h[i] = sum;
  241.    }
  242.  
  243.       /* constant = new # of gray levels div by area */
  244.    constant = new_grays/area;
  245.    for(i=0; i<ROWS; i++){
  246.       for(j=0; j<COLS; j++){
  247.          k           = image[i][j];
  248.          image[i][j] = sum_of_h[k] * constant;
  249.       }
  250.    }
  251. }  /* ends perform_histogram_equalization */
  252.  
  253.  
  254.  
  255.  
  256.         /*****************************************
  257.         *
  258.         *   zero_histogram(...
  259.         *
  260.         *   This function clears or zeros a
  261.         *   histogram array.
  262.         *
  263.         ******************************************/
  264.  
  265. zero_histogram(histogram)
  266.    unsigned long histogram[];
  267. {
  268.    int i;
  269.    for(i=0; i<=GRAY_LEVELS; i++)
  270.       histogram[i] = 0;
  271. }  /* ends zero_histogram */
  272.  
  273.  
  274.  
  275.  
  276.         /*****************************************
  277.         *
  278.         *   calculate_histogram(...
  279.         *
  280.         *   This function calculates the histogram
  281.         *   for an input image arry.
  282.         *
  283.         ******************************************/
  284.  
  285. calculate_histogram(image, histogram)
  286.    short  image[ROWS][COLS];
  287.    unsigned long histogram[];
  288. {
  289.    int i,j,k;
  290.    for(i=0; i<ROWS; i++){
  291.       for(j=0; j<COLS; j++){
  292.          k = image[i][j];
  293.          histogram[k] = histogram[k] + 1;
  294.       }
  295.    }
  296. }  /* ends calculate_histogram */
  297.  
  298.  
  299.  
  300.  
  301.         /******************************************
  302.         *
  303.         *   show_histogram(histogram)
  304.         *
  305.         *   This function shows the histogram
  306.         *   on the screen as numbers and stars.
  307.         *
  308.         *******************************************/
  309.  
  310. show_histogram(histogram)
  311.         unsigned long histogram[];
  312. {
  313.         int     count,
  314.                 i,
  315.                 j;
  316.         unsigned long max, scale;
  317.  
  318.  
  319.         max   = 0;
  320.         count = 0;
  321.  
  322.         for(i=0; i<GRAY_LEVELS; i++)
  323.            if(histogram[i] > max)
  324.               max = histogram[i];
  325.  
  326.         if(max > (70 - 12))
  327.            scale = max/(70 - 12);
  328.         else
  329.            scale = 1;
  330.  
  331.         printf("\n max=%ld scale=%ld",max, scale);
  332.  
  333.         printf("\n\ngray    count");
  334.         printf("\nlevel");
  335.  
  336.         for(i=0; i<256; i++){
  337.            if(histogram[i] == 0)
  338.               ++count;
  339.            else
  340.               count = 0;
  341.  
  342.